const logger = require('../config/logger');
const { allowedFileTypes } = require('../config/upload');

class UploadController {
  // 单文件上传
  static async singleUpload(req, res) {
    try {
      if (!req.file) {
        return res.status(400).json({ message: '请选择要上传的文件' });
      }
      const fileInfo = {
        originalName: req.file.originalname,
        filename: req.file.filename,
        path: req.file.path,
        size: req.file.size,
        mimetype: req.file.mimetype
      };

      res.json({
        message: '文件上传成功',
        file: fileInfo
      });
    } catch (error) {
      logger.error('文件上传失败:', error);
      res.status(500).json({ message: '文件上传失败' });
    }
  }

  // 多文件上传
  static async multipleUpload(req, res) {
    try {
      if (!req.files || req.files.length === 0) {
        return res.status(400).json({ message: '请选择要上传的文件' });
      }

      const filesInfo = req.files.map(file => ({
        originalName: file.originalname,
        filename: file.filename,
        path: file.path,
        size: file.size,
        mimetype: file.mimetype
      }));

      res.json({
        message: '文件上传成功',
        files: filesInfo
      });
    } catch (error) {
      logger.error('文件上传失败:', error);
      res.status(500).json({ message: '文件上传失败' });
    }
  }
}

module.exports = UploadController;